package com.xdh.zzqh.ui.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.xdh.zzqh.R
import com.xdh.zzqh.store.Product
import com.xdh.zzqh.ui.BaseActivity
import com.xdh.zzqh.ui.dialog.ChangeProductDialog
import com.xdh.zzqh.ui.dialog.CreateProductDialog
import com.xdh.zzqh.utils.onClick
import com.xdh.zzqh.utils.runOnUiWithInit
import com.xdh.zzqh.utils.showToast
import com.xdh.zzqh.utils.stringById
import kotlinx.coroutines.CompletableDeferred

class ProductFragment : Fragment() {

    private lateinit var mBtnCreate: Button
    private lateinit var mRv: RecyclerView

    private val mAdapter = ProductRvAdapter()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_product, null)
        mBtnCreate = view.findViewById(R.id.id_product_btn_create2)
        mRv = view.findViewById(R.id.id_product_rv)

        init()
        return view
    }

    private fun init() {
        mRv.adapter = mAdapter
        mRv.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

        mBtnCreate.onClick {
            try {
                val product = CreateProductDialog().waitCreate()
                product.save()
                mAdapter.update()
            } catch (e: Exception) {
                e.printStackTrace()
                showToast(e.message ?: stringById(R.string.cuo_wu))
            }
        }
    }


    override fun onResume() {
        super.onResume()
        runOnUiWithInit { mAdapter.update() }
    }

}

private class ProductItem(view: View) : RecyclerView.ViewHolder(view) {

    private val tv = view.findViewById<TextView>(R.id.id_item_product_tv_style)
    private val btnDelete = view.findViewById<Button>(R.id.id_item_product_btn_delete)
    private val btnChange = view.findViewById<Button>(R.id.id_item_product_btn_change)

    fun setUp(product: Product, delete: suspend () -> Unit, change: suspend () -> Unit) {
        tv.text = product.name
        btnDelete.onClick(delete)
        btnChange.onClick(change)
    }
}

private class ProductRvAdapter : RecyclerView.Adapter<ProductItem>() {

    private var list = emptyList<Product>()

    @SuppressLint("NotifyDataSetChanged")
    suspend fun update() {
        list = Product.queryAll()
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ProductItem, position: Int) {
        val product = list[position]
        holder.setUp(product, {
            Product.delete(product.name)
            update()
        }, {
            change(product)
        })
    }

    private suspend fun change(product: Product) {

        try {
            val p2 = ChangeProductDialog().waitChange(product)
            Product.delete(product.name)
            p2.save()
            update()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductItem {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_product, parent, false)
        return ProductItem(view)
    }
}




















